#include "systemtray.h"

SystemTray::SystemTray(QObject *parent): QObject(parent){
    // 创建系统托盘图标
    trayIcon = new QSystemTrayIcon(this);
    trayIcon->setIcon(QIcon("image2.png")); // 使用资源文件中的图标
    trayIcon->setToolTip("方寸修行\n双击登出");

    // 创建托盘菜单
    QMenu *trayMenu = new QMenu();
    QAction *quitAction = new QAction("退出", this);
    QAction *tOffClipboardAction = new QAction("关闭对话窗口", this);
    QAction *newStickyNoteAction = new QAction("新建便签", this);
    QAction *openStickyNoteWallAction = new QAction("打开便签墙", this);
    QAction *warnCheckAction = new QAction("便签提醒", this);
    warnCheckAction->setCheckable(true);//设置可选中
    QString warnChecked=localConfig.readLocalConfig("warnchecked"); // 调用ClassB的方法
    if(warnChecked=="true"){
        warnCheckAction->setChecked(true);
    }else{
        warnCheckAction->setChecked(false);
    }
    QAction *searchPartnerAction = new QAction("找个伙伴聊天", this);
    //QAction *xjbqzAction = new QAction("新建便签纸", this);
    connect(quitAction, &QAction::triggered, qApp, &QApplication::quit);
    connect(tOffClipboardAction, &QAction::triggered, this, &SystemTray::onTOffClipboardActionActivated);
    connect(newStickyNoteAction, &QAction::triggered, this, &SystemTray::onNewStickyNoteActivated);
    connect(openStickyNoteWallAction, &QAction::triggered, this, &SystemTray::onOpenStickyNoteWallActivated);
    connect(warnCheckAction, &QAction::triggered, this, &SystemTray::onWarnCheckActivated);
    connect(searchPartnerAction, &QAction::triggered, this, &SystemTray::onSearchPartnerActivated);
    //connect(xjbqzAction, &QAction::triggered, this, &SystemTray::onXjbqzActionActivated);
    trayMenu->addAction(searchPartnerAction);
    trayMenu->addAction(tOffClipboardAction);
    trayMenu->addAction(newStickyNoteAction);
    trayMenu->addAction(openStickyNoteWallAction);
    trayMenu->addAction(warnCheckAction);
    trayMenu->addAction(quitAction);
    //trayMenu->addAction(xjbqzAction);
    trayIcon->setContextMenu(trayMenu);
    connect(trayIcon, &QSystemTrayIcon::activated, this, &SystemTray::onTrayIconActivated);    // 显示托盘图标
    trayIcon->show();
}

void SystemTray::onTrayIconActivated(QSystemTrayIcon::ActivationReason reason)
{
    switch (reason)
    {
    case QSystemTrayIcon::Trigger:
        // 左键点击
        emit iconClicked();
        break;
    case QSystemTrayIcon::DoubleClick:
        // 双击
        emit iconDoubleClicked();
        break;
    /*case QSystemTrayIcon::MiddleClick:
        // 中键点击
        qDebug() << "Tray icon middle clicked";
        break;*/
    default:
        break;
    }
}

void SystemTray::onTOffClipboardActionActivated(){
    emit tOffClipboardClicked();
}

void SystemTray::onNewStickyNoteActivated(){
    emit newStickyNoteClicked();
}

void SystemTray::onOpenStickyNoteWallActivated(){
    emit openStickyNoteWallClicked();
}

void SystemTray::onWarnCheckActivated(){
    QString warnChecked=localConfig.readLocalConfig("warnchecked");
    if(warnChecked=="true"){
        localConfig.writeLocalConfig("warnchecked","false");
    }else{
        localConfig.writeLocalConfig("warnchecked","true");
    }
    emit warnCheckClicked();
}

void SystemTray::onSearchPartnerActivated(){
    emit searchPartnerClicked();
}

bool SystemTray::setUserPhoto(int photo){
    if(photo>1)trayIcon->setIcon(QIcon("image.png"));
    else trayIcon->setIcon(QIcon("image2.png"));
    return true;
}

